package com.weiaibenpao.demo.chislim.hurui.huanxin.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.ui.EaseChatFragment;
import com.hyphenate.easeui.widget.chatrow.EaseCustomChatRowProvider;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.hurui.activity.HBaseActivity;
import com.weiaibenpao.demo.chislim.hurui.huanxin.Constant;
import com.weiaibenpao.demo.chislim.hurui.huanxin.runtimepermissions.PermissionsManager;
import com.weiaibenpao.demo.chislim.hurui.utils.AppSPUtils;

public class ChatActivity extends HBaseActivity implements EaseChatFragment.EaseChatFragmentHelper{

    private static final int ITEM_VOICE_CALL = 13;

    public static ChatActivity activityInstance;
    private EaseChatFragment chatFragment;
    String toChatUsername;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        //new出EaseChatFragment或其子类的实例
        activityInstance = this;
        chatFragment = new EaseChatFragment();
        toChatUsername = getIntent().getExtras().getString("userId");
        //传入参数
       /* Bundle args = new Bundle();
        args.putInt(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_GROUP);
        args.putString(EaseConstant.EXTRA_USER_ID, getIntent().getStringExtra(EaseConstant.EXTRA_USER_ID));
        chatFragment.setArguments(args);*/
        chatFragment.setArguments(getIntent().getExtras());
        chatFragment.setChatFragmentListener(this);
        getSupportFragmentManager().beginTransaction().add(R.id.contain, chatFragment).commit();
    }

    @Override
    public void onSetMessageAttributes(EMMessage message) {

        // 通过扩展属性，将userPic和userName发送出去。
        String userPic = (String) AppSPUtils.get(ChatActivity.this , "logoUrl", "");
        if (!TextUtils.isEmpty(userPic)) {
            message.setAttribute("userPic" , userPic);
        }
        String userName = (String) AppSPUtils.get(ChatActivity.this ,"name", "");
        if (!TextUtils.isEmpty(userName)) {
            message.setAttribute("userName", userName);
        }
    }

    @Override
    public void onEnterToChatDetails() {

        if (chatFragment.chatType == Constant.CHATTYPE_GROUP) {
            EMGroup group = EMClient.getInstance().groupManager().getGroup(toChatUsername);
            if (group == null) {
                Toast.makeText(this, R.string.gorup_not_found, Toast.LENGTH_SHORT).show();
                return;
            }
            startActivityForResult(
                    (new Intent(this, GroupDetailsActivity.class).putExtra("groupId", toChatUsername)),
                    13);
        }else if(chatFragment.chatType == Constant.CHATTYPE_CHATROOM){
           // startActivityForResult(new Intent(this, ChatRoomDetailsActivity.class).putExtra("roomId", toChatUsername), REQUEST_CODE_GROUP_DETAIL);
        }

    }

    @Override
    public void onAvatarClick(String username) {

    }

    @Override
    public void onAvatarLongClick(String username) {

    }

    @Override
    public boolean onMessageBubbleClick(EMMessage message) {
        return false;
    }

    @Override
    public void onMessageBubbleLongClick(EMMessage message) {

    }

    @Override
    public boolean onExtendMenuItemClick(int itemId, View view) {
        return false;
    }

    @Override
    public EaseCustomChatRowProvider onSetCustomChatRowProvider() {
        return null;
    }

    @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                                     @NonNull int[] grantResults) {
        PermissionsManager.getInstance().notifyPermissionsChange(permissions, grantResults);
    }

}
